package org.epanetgrid.otimizacao.ag;

import java.util.Iterator;

import org.jheuristics.Individual;
import org.jheuristics.ga.DefaultPopulation;
import org.jheuristics.ga.GAConfig;
import org.jheuristics.ga.GAStatus;
import org.jheuristics.ga.Population;
import org.jheuristics.ga.operators.Selector;

/**
 * Classe que retorna os indiv�duos invi�veis, que tem algum alarme, de uma popula��o.
 * @author Marcell Manfrin, marcell at ourgrid.org, 2005
 */
public class InviaveisSelector implements Selector {

	/**
	 *
	 * @param population Popula��o onde a sele��o sera feita
	 * @param howMany Quantidade m�xima de indiv�duos a serem selecionados
	 * @param status Status do AG
	 * @param config Configura��o atual do AG
	 * @return Retorna indiv�duos invi�veis presentes na popula��o
	 * @see org.jheuristics.ga.operators.Selector#select(org.jheuristics.ga.Population, int, org.jheuristics.ga.GAStatus, org.jheuristics.ga.GAConfig)
	 */
	public Individual[] select(Population population, int howMany, GAStatus status, GAConfig config) {
        if (null == population) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
        if (0 == population.size()) {
        	return new Individual[0];
        }
        Population result = new DefaultPopulation();
		for (Iterator it = population.iterator(); it.hasNext();) {
			Individual individual = (Individual) it.next();
			SimpleFitness fitness = (SimpleFitness) individual.getFitness();
			if (fitness.getIntervaloDeErro() != -1) {
				result.addIndividual(individual);
			}
		}
		return result.toIndividualArray();
	}

}
